const { defineConfig } = require('@vue/cli-service');

const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: IS_PROD ? './' : '/',
  outputDir: 'dist',
  assetsDir: 'static',

  parallel: false,
  productionSourceMap: false,

  devServer: {},

  configureWebpack: config => {
    // 环境判断
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...
      // config.optimization.minimizer[0].options.minimizer.options.compress =
      //   Object.assign(
      //     config.optimization.minimizer[0].options.minimizer.options.compress,
      //     {
      //       drop_console: true,
      //       drop_debugger: true,
      //       pure_funcs: ['console.log'], // 移除console
      //     }
      //   );
      config.plugins = [
        ...config.plugins,
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: /\.(js|css|html)$/, //匹配文件名
          threshold: 10240,
          minRatio: 0.8,
        }),
        new UglifyJsPlugin({
          uglifyOptions: {
            output: {
              comments: false, // 去掉注释
            },
            warnings: false,
            compress: {
              drop_console: true,
              drop_debugger: true,
              pure_funcs: ['console.log'], //移除console
            },
          },
        }),
      ];
    } else {
      // 为开发环境修改配置...
    }
  },
});
